using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class GroundObject : Actor, ChangeStateable
    {

        public GroundObject(World world, Stage stage)
            : base(world, stage)
        {
        }

        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            //bodyDef.type = BodyType.Dynamic;
            bodyDef.position = new Vector2(0, 5);
            _body = world.CreateBody(bodyDef);
            _body._userData = "ground";
            _body.Owner = this;
            _body._type = BodyType.Static;

        }


        protected override void ConfigFilter()
        {
            base.ConfigFilter();
            _filter.categoryBits = GroundCategory;
            _filter.maskBits = GroundMask;
        }


        public override void Update(int delta)
        {
            CurrentAnimation.LayerDepth = 0.8f;
            CurrentAnimation.IsReplay = false;
            base.Update(delta);
            //if (( GameStage.player.Position.Y
            //    //+Math.Abs(Size.Y*Math.Cos(Rotation)/2) 
            //    //+Math.Abs(Size.X*Math.Sin(Rotation)/2)
            //    -Position.Y)>10) {
            //        _filter.maskBits = GroundMask ^ PlayerCategory;               
            //}
            //else
            //    _filter.maskBits = GroundMask;

            float pY1 = GameStage.player.Position.Y + (GameStage.player.Size.Y / 2)-2.5f;
            float y0 = (float)(Position.Y
                - Math.Abs(Size.Y * Math.Cos(Rotation) / 2)
            - Math.Abs(Size.X * Math.Sin(Rotation) / 2)
            );

            if (pY1 < y0)
                _filter.maskBits = GroundMask;
            else
                _filter.maskBits = GroundMask ^ PlayerCategory;

            if(Rotation!=0||Size.Y>=20)
                _filter.maskBits = GroundMask;

            _fixture.SetFilterData(ref _filter);

        }

        public virtual void ChangeState()
        {
            //throw new NotImplementedException();
        }

        public virtual void ChangeState(bool active)
        {
            //throw new NotImplementedException();
        }
    }
}
